package yaohua.main;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.jsoup.nodes.Document;

import yaohua.entity.Entity_help;
import yaohua.jsoup.Jsoup_help;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.SimpleAdapter;
import android.widget.Toast;

/**
 * 搜索书籍的详细信息列表界面
 * 
 * @author yh
 * 
 */
public class SearchItemActivity extends ListActivity
	{
		private Button store;
		ArrayList<String> storeUrl = null;
		int position = -1;
		String result;
		ProgressDialog progressDialog;

		@Override
		protected void onCreate(Bundle savedInstanceState)
			{
				super.onCreate(savedInstanceState);
				// 设置无标题
				requestWindowFeature(Window.FEATURE_NO_TITLE);
				setContentView(R.layout.searchitem);
				// 获得组件
				store = (Button) findViewById(R.id.store);
				store.setOnClickListener(new StoreListener());
				// 获取数据
				Intent intent = getIntent();
				ArrayList listItem = intent.getParcelableArrayListExtra("listItem");
				storeUrl = intent.getStringArrayListExtra("url");
				position = intent.getIntExtra("position", -1);
				// 初始化界面
				SimpleAdapter sa = new SimpleAdapter(this, listItem, android.R.layout.simple_list_item_2, new String[]
					{ "title", "value" }, new int[]
					{ android.R.id.text1, android.R.id.text2 });
				this.setListAdapter(sa);

			}

		@Override
		public boolean onOptionsItemSelected(MenuItem item)
			{
				new AlertDialog.Builder(this).setTitle("关于").setMessage("本程序为中北大学图书馆使用,欢迎您能向作者提供在使用过程中的bug,以方便作者进行修改.联系作者新浪微薄@耀华info")
						.show();
				return super.onOptionsItemSelected(item);
			}

		@Override
		public boolean onCreateOptionsMenu(Menu menu)
			{
				menu.add("关于");
				return super.onCreateOptionsMenu(menu);
			}

		class StoreListener implements OnClickListener
			{

				public void onClick(View v)
					{

						progressDialog = ProgressDialog.show(SearchItemActivity.this, "正在查询", "请稍等...", false, true);
						new Thread(new Runnable()
							{

								public void run()
									{

										try
											{
												Entity_help entity_help = new Entity_help();
												HttpResponse httpResponse = entity_help.getResponse(storeUrl.get(position));
												entity_help.handleBook(httpResponse, handler, "tdborder4", progressDialog);
											}
										catch (Exception e)
											{
												e.printStackTrace();
											}
										finally
											{
												progressDialog.dismiss();
											}
									}

							}).start();

					}

				// handler处理主线程
				Handler handler = new Handler()
					{

						@Override
						public void handleMessage(Message msg)
							{
								super.handleMessage(msg);
								if (msg.arg1 == -1)
									{
										progressDialog.dismiss();
										Toast.makeText(getApplicationContext(), "网络连接不可用，请检查网络设置", Toast.LENGTH_SHORT).show();
										msg.arg1 = 0;
									}
								else
									{
										Bundle bundle = msg.getData();
										// ArrayList<String> books =
										// bundle.getStringArrayList("books");此处已经没有必要了
										ArrayList<String> dateList = bundle.getStringArrayList("list");
										result = bundle.getString("result");

										// 处理数据生成listItem，每行显示两行数据
										// 测试Jsoup的方法
										Jsoup_help jsoup_help = new Jsoup_help();
										Document doc = jsoup_help.getDocument(result);
										// 得到标题的list
										List<String> listTitle = jsoup_help.geTextListByClass(doc, "tdborder2");
										// 创建带有Map的List，用于传送给ItemActivity
										List<Map<String, String>> listItem = new ArrayList<Map<String, String>>();
										for (int i = 2, j = 3; i < dateList.size() & j < dateList.size(); i = i + 5, j = j + 5)
											{
												Map<String, String> map = new HashMap<String, String>();
												map.put("title", dateList.get(i));
												map.put("value", dateList.get(j));
												listItem.add(map);
											}
										// 跳转到StorebooksActvity
										Intent intent = new Intent();
										intent.putParcelableArrayListExtra("listItem", (ArrayList<? extends Parcelable>) listItem);
										intent.setClass(SearchItemActivity.this, ItemActivity.class);
										startActivity(intent);

									}
							}

					};

			}

	}
